<?php

//use yii\web\View;

use yii\helpers\Url;


/**
 * @var TYPE_NAME $page
 * @var TYPE_NAME $tasks
 * @var TYPE_NAME $run
 */

echo $this->render('/layouts/top');

?>
<script type="text/javascript" src="/js/LimitTask.js?<?php echo rand()?>"></script>
<div style="margin:0px auto; width:1000px;">
    任务名称：<?php echo $detail["name"]?><br>
    标识identify：<?php echo $detail["impl_class"]?><br>
    列表页地址：<?php echo $detail["pagedata"]?><br>
    每页数据：<?php echo $detail["pagedata"]?><br>
    总页数：<?php echo $detail["page_sum"]?><br>

    列表页：
</div>
<br>
<div style="margin:0px auto; width:1000px;">
    <table class="table table-bordered">
        <tbody>
        <tr>
            <td width="200">当前抓取页数：</td>
            <td><?php echo $page?></td>
        </tr>
        <tr>
            <td width="200">当前页任务总数：</td>
            <td><?php echo ($task_city+1)."/".$city_total?><span id="task_total"></span></td>
        </tr>
        <tr>
            <td>任务进度：</td>
            <td id="task_progress">0/<?php echo $task_total?></td>
        </tr>
        </tbody>
    </table>
</div>

<div style="margin:0px auto; width:1000px;">
    <div id="showinfo" style="width:100%; height:200px; border:1px solid #CCC; overflow-y:auto"></div>
    <div style="height:50px; line-height:50px;text-align:center">
        <button id="button_start" type="button" class="btn btn-primary" name="submit-button">开始</button>
        <button id="button_stop" type="button" class="btn btn-primary" name="submit-button">停止</button>
    </div>
</div>

<!-- JS -->
<script language="javascript">

    var task_id = <?php echo $detail["id"]?>;
    var tasks = <?php echo json_encode($tasks, JSON_UNESCAPED_UNICODE);?>;
    var page = <?php echo intval($page)?>;
    var page_sum = <?php echo intval($detail["page_sum"])?>;
    var run = <?php echo intval($run)?>;

    var mt = new LimitTask(tasks, 100);

    function addinfo(info, color){
        if(!arguments[1])color = '#000000';

        var text = $('#showinfo').html();
        info = '<div style="color:'+color+'">'+info+'</div>';
        text = text + info;

        $('#showinfo').html(text);
        $('#showinfo')[0].scrollTop = $('#showinfo')[0].scrollHeight;//滚动条在最下方
    }


    mt.onTask = function(key, task, isLast){
        console.log(key, task);
        console.log("正在执行任务："+(key+1), task["name"]);

        $.ajax({
            url : "<?php echo Url::to(["on-task", "id"=>$detail["id"]])?>",
            data : task,
            type : 'POST',
            dataType : 'JSON',
            success : function (result) {
                console.log(result);
                if(result.code == 200){
                    let msg = "任务："+(key+1)+"  完成！";
                    addinfo(msg, '#009900');
                    $('#task_progress').html(""+(key+1)+"/"+tasks.length);
                    if(isLast){
                        addinfo("第"+page+"页完成！", '#009900');
                        if(page >= page_sum){
                            addinfo("任务完成！", '#009900');
                        }else{
                            setTimeout(function (){
                                window.location.href = "?id="+task_id+"&page="+parseInt(page+1)+"&run=1";
                            }, 1000)
                        }
                    }else{
                        mt.next();
                    }
                }else{
                    addinfo(result.info+"失败");
                    addinfo("任务 "+key+" 超时，5秒后自动重试");
                    mt.retry();
                }
            }
        });
    }


    $(document).ready(function () {
        $('#button_start').on('click', function () {
            mt.start();
        });

        $('#button_stop').on('click', function () {
            mt.stop();
        });

        $('#task_total').html(tasks.length);

        if(run){
            mt.start();
        }
    });

</script>

<?php echo $this->render('/layouts/footer');?>

